package com.helmlabs.grouper.accounts.model;

import java.util.List;

import javax.persistence.Embedded;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToMany;
import javax.persistence.Transient;

import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.springframework.security.GrantedAuthority;
import org.springframework.security.GrantedAuthorityImpl;
import org.springframework.security.userdetails.UserDetails;

import com.helmlabs.grouper.search.model.Indexable;

@Entity
public class UserProfile implements UserDetails, Indexable {

	private Long id;
	
	private String title;
	private String firstName;
	private String lastName;
	private String middleName;
	private String suffix;
	private Address primaryAddress;
	
	private String referredBy;
	private String industry;
	private String ftzNumber;
	
	private String email;
	private String phone;
	private String fax;
	
	private boolean accountNonLocked;
	private boolean accountNonExpired;
	private boolean credentialsNonExpired;
	private boolean enabled;
	
	private List<Role> roles;
	
	private String username;
	private String password;
	private GrantedAuthority[] authorities;
	
	@Id
	@GeneratedValue(strategy=GenerationType.AUTO)
	public Long getId() {
		return id;
	}
	public void setId(Long id) {
		this.id = id;
	}
	public boolean isAccountNonLocked() {
		return accountNonLocked;
	}
	public void setAccountNonLocked(boolean accountNonLocked) {
		this.accountNonLocked = accountNonLocked;
	}
	public boolean isAccountNonExpired() {
		return accountNonExpired;
	}
	public void setAccountNonExpired(boolean accountNonExpired) {
		this.accountNonExpired = accountNonExpired;
	}
	public boolean isCredentialsNonExpired() {
		return credentialsNonExpired;
	}
	public void setCredentialsNonExpired(boolean credentialsNonExpired) {
		this.credentialsNonExpired = credentialsNonExpired;
	}
	public boolean isEnabled() {
		return enabled;
	}
	public void setEnabled(boolean enabled) {
		this.enabled = enabled;
	}
	public String getUsername() {
		return username;
	}
	public void setUsername(String username) {
		this.username = username;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	public String getFirstName() {
		return firstName;
	}
	public void setFirstName(String firstName) {
		this.firstName = firstName;
	}
	public String getLastName() {
		return lastName;
	}
	public void setLastName(String lastName) {
		this.lastName = lastName;
	}
	public String getMiddleName() {
		return middleName;
	}
	public void setMiddleName(String middleName) {
		this.middleName = middleName;
	}
	@Transient
	public GrantedAuthority[] getAuthorities() {
		if (roles!=null){
			authorities = new GrantedAuthority[roles.size()];
			
			for (int i=0; i<roles.size(); i++){
				authorities[i] = new GrantedAuthorityImpl(roles.get(i).getName());
			}
		}
		return authorities;
	}
	
	

	public String getTitle() {
		return title;
	}
	public void setTitle(String title) {
		this.title = title;
	}
	public String getSuffix() {
		return suffix;
	}
	public void setSuffix(String suffix) {
		this.suffix = suffix;
	}
	@ManyToMany(fetch=FetchType.EAGER)
	public List<Role> getRoles() {
		return roles;
	}
	public void setRoles(List<Role> roles) {
		this.roles = roles;
	}
	
	@Embedded
	public Address getPrimaryAddress() {
		if (primaryAddress==null){
			return new Address();
		}
		return primaryAddress;
	}
	public void setPrimaryAddress(Address primaryAddress) {
		this.primaryAddress = primaryAddress;
	}
	public String getEmail() {
		return email;
	}
	public void setEmail(String email) {
		this.email = email;
	}
	public String getPhone() {
		return phone;
	}
	public void setPhone(String phone) {
		this.phone = phone;
	}
	public String getFax() {
		return fax;
	}
	public void setFax(String fax) {
		this.fax = fax;
	}
	
	public String getReferredBy() {
		return referredBy;
	}
	public void setReferredBy(String referredBy) {
		this.referredBy = referredBy;
	}
	public String getIndustry() {
		return industry;
	}
	public void setIndustry(String industry) {
		this.industry = industry;
	}
	public String getFtzNumber() {
		return ftzNumber;
	}
	public void setFtzNumber(String ftzNumber) {
		this.ftzNumber = ftzNumber;
	}


	public Document toDocument(){
		Document doc = new Document();
		doc.add(new Field("id", String.valueOf(id), Field.Store.YES, Field.Index.UN_TOKENIZED));
		doc.add(new Field("type", String.valueOf(UserProfile.class), Field.Store.YES, Field.Index.TOKENIZED));
		doc.add(new Field("general", this.toString(), Field.Store.NO, Field.Index.TOKENIZED));
		doc.add(new Field("firstName", firstName, Field.Store.YES, Field.Index.TOKENIZED));
		doc.add(new Field("lastName", lastName, Field.Store.YES, Field.Index.TOKENIZED));	
		return doc;
	}
	
	public String toString(){
		return super.toString() + "{id=" + getId() + ",email=" + getEmail() + "}";
	}

}
